{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "import time\r\n",
    "from GroverEq import GroverEq\r\n",
    "from BoolEquations import BoolEquations\r\n",
    "\r\n",
    "time_start = time.time()\r\n",
    "num_variable = 21\r\n",
    "print(\"运行规模: {:d}变元\".format(num_variable))\r\n",
    "# 类对象\r\n",
    "equations = BoolEquations(num_variable)\r\n",
    "print(\"正在生成方程组及经典破解中...（请耐心等待）\")\r\n",
    "# 运行算法，返回具有唯一解的方程组\r\n",
    "equations.run()\r\n",
    "\r\n",
    "# 打印方程组\r\n",
    "equations.print_equations()\r\n",
    "print(\"正在经典算法求解中...\")\r\n",
    "print(\"经典算法：\")\r\n",
    "# 打印结果\r\n",
    "print(equations.result_set)\r\n",
    "time_end = time.time()\r\n",
    "print('经典算法运行时间:%fs' % (time_end - time_start))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "运行规模: 21变元\n",
      "正在生成方程组及经典破解中...（请耐心等待）\n",
      "============方程组============\n",
      "[(1, 14), (9, 15), (14, 16), (1, 9), (1, 8), (4, 7), (8, 12), (5, 8), (4, 8), (6, 6), (12, 15), 4, (16, 17), (7, 20), (9, 20), 12, (15, 15), (3, 18), (7, 11)]\n",
      "[(3, 16), (1, 16), (11, 20), (8, 14), (0, 17), (9, 14), (6, 9), (14, 20), (16, 18), (5, 7), (16, 19), (2, 17), (10, 11), (0, 14), (2, 6), (3, 14), (11, 18), (3, 6), (1, 2)]\n",
      "[(4, 11), (17, 19), (7, 10), (11, 13), (3, 7), (4, 13), (7, 11), (9, 16), (5, 17), (2, 9), (8, 10), (10, 15), (0, 13), (0, 5), (4, 10), (12, 17), (2, 16), (10, 13), (9, 18)]\n",
      "[(2, 10), (2, 4), (18, 20), (0, 2), (5, 16), (16, 20), (1, 2), (4, 4), (3, 18), (15, 18), (1, 7), (5, 12), (14, 17), (15, 15), (6, 19), 4, (1, 1), (1, 12), (0, 15)]\n",
      "[(8, 12), (11, 14), (6, 16), (6, 15), (4, 17), (3, 19), (9, 18), (8, 16), (3, 6), 11, (0, 0), (0, 3), (6, 8), (11, 18), (4, 18), (2, 17), (10, 18), (5, 19), (7, 15)]\n",
      "[(6, 9), 6, (3, 12), 19, (0, 18), (12, 15), (0, 15), (7, 12), (10, 13), 15, (2, 12), (3, 10), (1, 6), (2, 16), (5, 6), (10, 15), (3, 8), (13, 17), (14, 19)]\n",
      "[(7, 11), 0, (19, 19), (1, 9), (2, 5), (16, 19), (7, 15), (1, 17), (1, 12), (14, 15), (10, 16), (9, 13), (20, 20), (1, 1), (6, 13), (5, 12), (3, 20), (0, 12), (9, 10)]\n",
      "[(1, 2), (8, 17), (5, 9), (8, 14), (6, 6), (8, 10), (14, 19), (16, 17), (5, 19), (4, 7), (12, 13), (1, 18), 5, (10, 18), 14, (0, 3), (4, 12), 8, (4, 16)]\n",
      "[(9, 18), (4, 15), (9, 11), (11, 13), (5, 7), (1, 7), (9, 10), (10, 11), (13, 17), (9, 14), (6, 7), (5, 14), (1, 8), (3, 8), 2, (3, 10), (1, 20), (3, 11), (7, 19)]\n",
      "[(0, 15), (1, 19), (6, 14), (0, 2), (2, 9), (0, 0), (4, 16), (0, 13), (0, 12), (4, 13), (10, 14), (1, 5), (0, 17), (3, 20), (3, 15), (9, 13), (7, 17), (18, 18), (7, 20)]\n",
      "[(7, 15), (2, 7), (1, 4), (6, 15), (15, 18), (2, 12), (7, 19), (14, 17), 8, (16, 20), (4, 6), 13, (3, 7), (4, 12), 9, (1, 18), (0, 20), (3, 9), (0, 14)]\n",
      "[(14, 19), (13, 19), (8, 20), 19, 11, (10, 16), (7, 20), (6, 8), (3, 11), (3, 19), (10, 10), (16, 16), (5, 16), (1, 20), (7, 13), (5, 14), (0, 8), (19, 20), (0, 5)]\n",
      "[(9, 10), (4, 14), (14, 18), (1, 9), 15, (12, 13), (0, 14), (1, 6), (18, 18), (12, 15), (11, 19), (13, 20), (4, 17), (7, 17), (2, 19), (1, 5), (3, 17), (13, 13), (15, 15)]\n",
      "[(4, 16), (3, 5), (2, 8), (8, 14), (14, 15), (6, 7), (0, 5), (20, 20), (3, 9), (16, 19), (2, 14), (0, 7), (0, 18), (0, 20), (5, 10), 13, (6, 12), (7, 18), (11, 18)]\n",
      "[(7, 19), (14, 14), (4, 9), (11, 13), (4, 7), (10, 14), (15, 15), 5, (19, 20), (8, 10), (12, 16), (7, 7), (2, 5), (0, 8), (10, 20), (16, 16), (15, 17), (2, 11), (2, 16)]\n",
      "[(7, 20), (13, 18), (5, 11), (0, 2), (9, 14), (4, 6), (11, 18), (1, 8), (13, 13), (1, 7), (4, 19), (3, 13), (6, 6), (3, 17), (17, 17), (13, 20), (17, 20), (5, 13), (1, 1)]\n",
      "[(0, 14), (0, 6), (9, 12), (6, 15), (4, 13), (10, 10), (2, 16), (0, 17), (7, 18), (0, 0), (6, 20), (5, 15), (5, 7), (6, 12), (2, 13), (0, 7), (9, 20), (0, 19), (10, 18)]\n",
      "[(0, 5), (2, 20), (4, 20), 19, (16, 20), (11, 19), (1, 1), (3, 7), (2, 11), (2, 12), 3, (2, 15), (2, 9), (15, 17), (13, 14), (7, 7), (2, 6), (10, 17), (3, 8)]\n",
      "[(15, 15), 10, (2, 18), (1, 9), (3, 19), (2, 14), (10, 18), (5, 16), (5, 13), (10, 16), (13, 16), (9, 15), (15, 18), (17, 20), (18, 19), (3, 13), (4, 10), (8, 15), (3, 20)]\n",
      "[(11, 18), (1, 13), (0, 16), (8, 14), (12, 15), (12, 16), (3, 8), (4, 17), (0, 19), (12, 13), (7, 16), (1, 16), 11, (9, 20), (0, 10), (5, 15), (6, 19), (8, 11), (4, 12)]\n",
      "[(2, 16), (17, 18), 18, (11, 13), (16, 19), (4, 19), (3, 20), (0, 18), (10, 17), (6, 7), (10, 19), (17, 19), 15, (2, 6), (1, 11), (2, 15), (4, 18), (14, 16), (1, 20)]\n",
      "============方程的值==========\n",
      "[0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1]\n",
      "==============================\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "eq_per_group = equations.split_integer()\r\n",
    "\r\n",
    "print(\"正在量子算法求解中...\")\r\n",
    "time_start = time.time()\r\n",
    "grover = GroverEq(num_variable, equations.eq_set, equations.eq_value, eq_per_group)\r\n",
    "\r\n",
    "grover.construct()\r\n",
    "answer = grover.run(shots=10240)\r\n",
    "\r\n",
    "print(\"量子算法：\")\r\n",
    "print(sorted(answer.items(),key=lambda x:x[1],reverse=True)[0])\r\n",
    "#print(answer)\r\n",
    "print(\"量子电路深度为：%d\" % grover.circuit.depth())\r\n",
    "time_end = time.time()\r\n",
    "print('量子电路运行时间:%fs' % (time_end - time_start))"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "grover.draw('mpl')"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "source": [
    "import random\r\n",
    "from time import time\r\n",
    "num_variable = 21\r\n",
    "most = int(num_variable*(num_variable+1)/2)\r\n",
    "list_one = [i for i in range(num_variable)]\r\n",
    "# 生成二变元集合\r\n",
    "list_two = [(i, j) for i in range(num_variable) for j in range(i, num_variable)]\r\n",
    "# 所有元素列表\r\n",
    "list_all_elem = list_one + list_two\r\n",
    "eq_set = []\r\n",
    "\r\n",
    "#随机生成方程组\r\n",
    "for _ in range(num_variable):\r\n",
    "    random.shuffle(list_all_elem)\r\n",
    "    # 设置随机数种子\r\n",
    "    random.seed(time())\r\n",
    "    num = random.randint(1,most)\r\n",
    "    eq_set.append(list_all_elem[:num])\r\n",
    "sum = 0\r\n",
    "for eq in eq_set:\r\n",
    "    sum += len(eq)\r\n",
    "sum"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "3564"
      ]
     },
     "metadata": {},
     "execution_count": 73
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "from BoolEquations import BoolEquations\r\n",
    "import time\r\n",
    "time_start = time.time()\r\n",
    "num_variable = 2\r\n",
    "eq = BoolEquations(num_variable)\r\n",
    "\r\n",
    "eq.run()\r\n",
    "sum = 0\r\n",
    "for eq in eq.eq_set:\r\n",
    "    sum += len(eq)\r\n",
    "sum\r\n",
    "time_end = time.time()\r\n",
    "print('生成方程组运行时间:%fs' % (time_end - time_start))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "============方程组============\n",
      "[1, (1, 1)]\n",
      "[(1, 1), 0]\n",
      "============方程的值==========\n",
      "[0, 1]\n",
      "==============================\n",
      "生成方程组运行时间:0.001992s\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "eq_set = [[(1, 9), (5, 7), (2, 6), (0, 7), (0, 1), (8, 9), (5, 9), (4, 7), (4, 9), (1, 7), (1, 4), 1, (4, 8), (3, 4), (2, 7), 7, (1, 2), (2, 4), (2, 3), (0, 5), (3, 5), (1, 8), (1, 6), (0, 6), (7, 8), (5, 8), (3, 6), (0, 8), 3, (3, 8), (1, 5), (6, 9), 4, (0, 4), (2, 8), 0, (6, 7), (0, 9), (5, 6), (2, 9), (6, 8)],\r\n",
    "        [(1, 9), (1, 2), 9, (4, 6), 2, (4, 7), (0, 9), (7, 9), (2, 5), (6, 9), (1, 5), (2, 4), (0, 3), (0, 4), 7, (3, 5), (4, 8), (0, 2), 1, (3, 7), (4, 9), 0, (2, 6), (2, 9), (1, 3), (3, 6), (5, 6), (5, 9), (2, 7), (1, 8), (5, 7), (7, 8), (1, 7), (2, 8), (0, 8), (5, 8), (0, 6), (0, 5), (0, 7), (6, 7), (1, 4)]\r\n",
    "        ]\r\n",
    "\r\n"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "interpreter": {
   "hash": "a4f87f393be014def60932b1b70194e13d5d744d1a8c01722c2bc9e3a60f4b90"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}